package me.helllp.book.netty.step10;

import org.jboss.marshalling.*;

import java.io.IOException;

/**
 * Marshalling工厂
 * 
 * @author Administrator
 *
 */
public final class MarshallingCodecFactory {

	/**
	 * 创建 Marshaller 对象，用于构建编码器 
	 * 
	 * @return
	 * @throws IOException
	 */
    protected static Marshaller buildMarshalling() throws IOException {
        //	通过 Marshalling 工具类的精通方法获取 Marshalling 实例对象，参数serial标识创建的是java序列化工厂对象。
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        
        //	创建了MarshallingConfiguration对象，配置了版本号为5
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        
        //	创建 Marshalling 对象
        Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
        
        return marshaller;
    }

    /**
     * 创建 Unmarshaller 对象，用于解码器构建
     * 
     * @return
     * @throws IOException
     */
    protected static Unmarshaller buildUnMarshalling() throws IOException {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        
        final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
        
        return unmarshaller;
    }
}